/*
* BurnController.java
* Created on  202015/6/15 16:34
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/15       longshihui    初始版本
*
*/

package com.infitecs.eyas.combustion;

import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.AirGasParameterService;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.result.model.MiddleResult;
import com.infitecs.eyas.result.service.MiddleResultService;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.modal.StepInfo;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/combustion/burn")
public class BurnController {
    @Autowired
    private BurnService burnService;

    @Autowired
    private SessionService sessionService;

    @Autowired
    private AirGasParameterService airGasParameterService;

    @Autowired
    private EfficiencyService efficiencyService;

    @Autowired
    private MiddleResultService middleResultService;

    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;


    @RequestMapping("")
    public String getBurnPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "combustion/burn";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "combustion/burn";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        CalculateBurn calculateBurn = burnService.getCalculateBurn(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("calculateBurn", calculateBurn);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "combustion/burn";
    }

    @RequestMapping(value = "/Re-reference", method = RequestMethod.POST)
    @ResponseBody
    public CalculateBurn reReference() {
        CalculateBurn calculateBurn = burnService.getCalculateBurn();
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(sessionService.getCaseId());
        Efficiency efficiency = efficiencyService.getEfficiency(sessionService.getCaseId());
        if (null != airGasParameter && null != efficiency) {
            double E45 = airGasParameter.getHearthOverOutAirCoefficient();
            double E93 = DoubleUtil.getInstance().get3Double(efficiency.getE93());
            double E52 = DoubleUtil.getInstance().get3Double(efficiency.getE52());
            double E46 = airGasParameter.getAirPreheaterAirLeakageCoefficient();
            double E28 = airGasParameter.getE28();
            calculateBurn.setE4(E93);
            calculateBurn.setE3(E45);
            calculateBurn.setE6(E52);
            calculateBurn.setE8(E28);
            calculateBurn.setE20(E46);
        }
        Double e54 = middleResultService.getMiddle(new MiddleResult(sessionService.getCaseId(), "E126")).getResult();
        if (null != e54) {
            calculateBurn.setE54(DoubleUtil.getInstance().get3Double(e54));
        }


        Double e88 = expressionService.calculate(new Param("RE88"));
        if (null != e88) {
            calculateBurn.setE88(e88);
        }
        burnService.saveVo(calculateBurn);
        return calculateBurn;
    }

    @RequestMapping(value = "/saveValueOfBurnEx", method = {RequestMethod.POST})
    @ResponseBody
    public void saveValueOfBurnEx(CalculateBurn calculateBurn) {
        burnService.saveValueOfBurn(calculateBurn);
    }

    @RequestMapping(value = "/getCalculateInit", method = {RequestMethod.POST})
    @ResponseBody
    public void getCalculateInit() {
        sessionService.putBurnValue(sessionService.getCaseId());
    }

    @RequestMapping(value = "burnSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse burnSave(CalculateBurn calculateBurn) {
        try {
            burnService.burnSave(calculateBurn);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }

}
